/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.thelostlands.shinycast;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

/**
 *
 * @author fabio
 */
class CmdHandler implements CommandExecutor{
    
    Message plugin;

    CmdHandler(Message aThis) {
        plugin=aThis;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String commandlabel, String[] args) {
        if (sender instanceof ConsoleCommandSender) {
			sender.sendMessage("Console non implementata");
                        return false;
		} else if (sender instanceof Player && Permissions.has((Player)sender,Permissions.Perms.ADMINMODE)) {                    
			if (command.getName().equals("message")) {
                            if(args==null || args[0]==null){
                                sender.sendMessage("Devi specificare un comando");
                                return false;
                            }
                            if("tempo".equals(args[0])){
                                try{
                                    int s = Integer.parseInt(args[1]);
                                    plugin.messaggero.setSeconds(s);
                                    plugin.messaggero.restart();
                                    
                                    plugin.saveConfigFile();
                                    sender.sendMessage("[Shinycast] Tempo del messaggio automatico cambiato");
                                    
                                    return true;
                                }
                                catch(Exception ex){
                                    sender.sendMessage("[Shinycast] "+args[1]+" non è un parametro di tempo valido");
                                    return false;
                                }
                            }else if("set".equals(args[0])){
                                int counter=2;
                                String total=args[1]==null ? "" : args[1];
                                while(counter<args.length){
                                    total+=" "+args[counter++];
                                }
                                plugin.messaggero.setMessage(total);
                                sender.sendMessage("[Shinycast] Messaggio cambiato correttamente");
                                plugin.saveConfigFile();
                                plugin.messaggero.restart();
                                return true;
                            } else if("on".equals(args[0])){
                                sender.sendMessage("[Shinycast] Riattivo il broadcast");
                                plugin.messaggero.restart();
                                return true;
                            } else if("off".equals(args[0])){
                                sender.sendMessage("[Shinycast] Spengo il broadcast");
                                plugin.messaggero.stop();
                                return true;
                            } else if("prefisso".equals(args[0])){
                                if(args.length<2){
                                    sender.sendMessage("Devi specificare il nuovo prefisso");
                                }else{
                                    plugin.messaggero.setPrefix(args[1]);
                                    plugin.saveConfigFile();
                                    plugin.messaggero.restart();
                                    return true;
                                }
                            }
                              else {
                                sender.sendMessage("[Shinycast] Opzione "+args[0]+" non attiva.");
                                return false;
                            }
                        }
                }
        return false;
    }
    
}
